package com.zjdiepu.www.biz.dh;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.dh.DhRemindLoanMapper;
import com.zjdiepu.www.ibiz.dh.IDhRemindLoanService;
import com.zjdiepu.www.model.dh.dto.DhRemindLoan;
import com.zjdiepu.www.model.dh.ext.DhRemindLoanExt;

/**
 * 放款还款提醒Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年07月24日 17时41分<br/>
 * @since 1.0<br/>
 */
@Service
public class DhRemindLoanServiceImpl extends BaseService implements IDhRemindLoanService {
	public static final Logger logger = LoggerFactory.getLogger(DhRemindLoanServiceImpl.class);

	@Autowired
	private DhRemindLoanMapper dhRemindLoanMapper;
	
	@Override
	public String getNextBusiId(Date thisTimeLending, String thisId) {
		return dhRemindLoanMapper.getNextBusiId(thisTimeLending, thisId);
	}
	
	@Override
	public String getPreviousBusiId(Date thisTimeLending, String thisId) {
		return dhRemindLoanMapper.getPreviousBusiId(thisTimeLending, thisId);
	}
	
	@Override
	public void saveUpdateAndDelete(DhRemindLoan dhRemindLoan, OperateType operType) {
		switch (operType) {
		case add:
			dhRemindLoan.initInsertData(dhRemindLoan.getModifier(), dhRemindLoan.getModifierName());
			dhRemindLoanMapper.insert(dhRemindLoan);
			break;
		case edit:
			dhRemindLoan.initUpdateData(dhRemindLoan.getModifier(), dhRemindLoan.getModifierName());
			// 影响的记录数
			int updateCount = dhRemindLoanMapper.updateByPrimaryKeySelective(dhRemindLoan);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dhRemindLoan.initDeleteData(dhRemindLoan.getModifier(), dhRemindLoan.getModifierName());
			// 影响的记录数
			int deleteCount = dhRemindLoanMapper.updateByPrimaryKeySelective(dhRemindLoan);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DhRemindLoan> selectByExt(DhRemindLoanExt dhRemindLoanExt) {
		return dhRemindLoanMapper.selectByExt(dhRemindLoanExt);
	}
	
	@Override
	public int countByExt(DhRemindLoanExt dhRemindLoanExt){
		return dhRemindLoanMapper.countByExt(dhRemindLoanExt);
	}
	
	@Override
	public DhRemindLoan selectById(String id) {
		return dhRemindLoanMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dhRemindLoanMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DhRemindLoan dhRemindLoan) {
		return dhRemindLoanMapper.countRefined(dhRemindLoan);
	}

	@Override
	public DhRemindLoan selectRefined(DhRemindLoan dhRemindLoan) {
		return dhRemindLoanMapper.selectRefined(dhRemindLoan);
	}
	
	@Override
	public int deletedRefined(DhRemindLoan dhRemindLoan) {
		return dhRemindLoanMapper.deletedRefined(dhRemindLoan);
	}

	@Override
	public boolean repeat(DhRemindLoan dhRemindLoan) {
		int count = countRefined(dhRemindLoan);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dhRemindLoan.getId())) {
			return true;
		}
		DhRemindLoan dbItem = selectRefined(dhRemindLoan);
		if(null == dbItem) {
			return false;
		}
		return !dhRemindLoan.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dhRemindLoanMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<DhRemindLoan> dhRemindLoans) {
		return dhRemindLoanMapper.insertBatch(dhRemindLoans);
	}
}
